package com.huitone.smspfm;

import java.io.FileInputStream;
import java.util.Arrays;

import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

import com.bin.common.utils.ArrayUtils;
import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.core.config.AppStaticConfigs;
import com.huitone.smspfm.core.listener.OutSideCommandsHandler;
import com.huitone.smspfm.core.listener.ShareMemoryCommandsListener;
import com.huitone.smspfm.core.smscallltest.SmsCallScanTask;
import com.huitone.smspfm.core.smscallltest.SmsCallTestScanTask;
import com.huitone.smspfm.core.wransms.WranSmsSocketTask;
import com.huitone.smspfm.exception.SimpleUncaughtExceptionHandler;
import com.huitone.smspfm.logger.ExtLogger;
import com.huitone.smspfm.task.ForbidProcessingTask;
import com.huitone.smspfm.task.GatewayAutoSwitchTask;
import com.huitone.smspfm.task.GetPriListLengthTask;
import com.huitone.smspfm.task.MoSmsProcessingTask;
import com.huitone.smspfm.task.MtSmsProcessingTask;
import com.huitone.smspfm.task.RebuildPriListTask;
import com.huitone.smspfm.task.ReloadConfigTimingTask;
import com.huitone.smspfm.task.RouteFaultCheckTask;
import com.huitone.smspfm.task.SMAffixZsetTask;

/**   
* @Title Main.java 
* @Package com.huitone.smspfm 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年9月16日 下午6:10:31 
* @version V1.0   
*/
public class Main {
	
	static {
		try {
//			String path = Thread.currentThread().getContextClassLoader().getResource("log4j2.xml").getPath().substring(1).replace("/", "\\\\");
//			Configurator.initialize(null, new ConfigurationSource(new FileInputStream(path)));

			Configurator.initialize(null, new ConfigurationSource(new FileInputStream(System.getProperty("log4j2.xml"))));
		} catch (Exception e) {
			System.out.println("初始化log4j2配置文件错误");
		}
	}
	
	private static final ExtLogger logger = ExtLogger.create(Main.class);
	
	private static ShareMemoryCommandsListener listener = null;
	
	private static GetPriListLengthTask getPriListLengthTask = null;
	
	private static ForbidProcessingTask forbidProcessingTask = null;
	
	private static MtSmsProcessingTask mtSmsProcessingTask = null;
	
	private static MoSmsProcessingTask moSmsProcessingTask = null;
	
	private static SMAffixZsetTask smAffixZsetTask = null;
	
	private static RebuildPriListTask rebuildPriListTask = null;
	
	private static ReloadConfigTimingTask reloadConfigTimingTask = null;
	
	private static RouteFaultCheckTask routeFaultCheckTask = null;
	
	private static GatewayAutoSwitchTask gatewayAutoSwitchTask = null;
	
	private static WranSmsSocketTask wranSmsSocketTask = null;
	
	private static SmsCallScanTask smsCallScanTask = null;

	public static void main(String[] args) {
		
		// 定义全局的未捕捉异常处理类
		Thread.setDefaultUncaughtExceptionHandler(new SimpleUncaughtExceptionHandler());
		
		// 初始化全局参数
		AppStaticConfigs.init();
		
		// 构建外部命令监听器,监听start,stop,reload等命令
		listener = new ShareMemoryCommandsListener();
		listener.setHandler(new OutSideCommandsHandler() {

			@Override
			public void stop(String[] args) {
				logger.info("stop:" + Arrays.asList(args));
				
				if (ArrayUtils.isEmpty(args)) {
					
					stopMtService();
					stopFbService();
					stopMoService();
					stopOtherTasks();
					
					listener.stopListener();
					
				} else {
					
					String param = StringUtils.trim(args[0]);
					
					if (StringUtils.equalsIgnoreCase(param, "mtservice")) {
						stopMtService();
					} else if (StringUtils.equals(param, "fbservice")) {
						stopFbService();
					} else if (StringUtils.equals(param, "moservice")) {
						stopMoService();
					} else if (StringUtils.equals(param, "othertasks")) {
						stopOtherTasks();
					}
				}
			}

			@Override
			public void start(String[] args) {
				logger.info("start:" + Arrays.asList(args));
				
				if (ArrayUtils.isEmpty(args)) {
					
					startOtherTasks();
					
				} else {
					
					String param = StringUtils.trim(args[0]);
					
					if (StringUtils.equalsIgnoreCase(param, "mtservice")) {
						startMtService();
					} else if (StringUtils.equals(param, "fbservice")) {
						startFbService();
					} else if (StringUtils.equals(param, "moservice")) {
						startMoService();
					} else if (StringUtils.equals(param, "othertasks")) {
						startOtherTasks();
					} else if (StringUtils.equals(param, "rbservice")) {
						rebuildPriList();
					}
				}
			}

			@Override
			public void reload(String[] args) {
				logger.info("reload:" + Arrays.asList(args));
				
				logger.info("短信平台预处理程序重新加载配置参数...");
				
				AppStaticConfigs.reload();
				
			}

			@Override
			public void handle(String[] args) {
				logger.info("handle:" + Arrays.asList(args));
			}
		});
		listener.startListener();
	}
	
	/**
	 * 启动下行短信处理服务
	 */
	public static void startMtService() {
		
		logger.info("短信平台下行短信处理服务正在启动...");
		
		if (mtSmsProcessingTask == null || !mtSmsProcessingTask.isAlive()) {
			mtSmsProcessingTask = new MtSmsProcessingTask();
			mtSmsProcessingTask.start();
		}
    	
		logger.info("短信平台下行短信处理服务启动完成");
	}
	
	/**
	 * 关闭下行短信处理服务
	 */
	public static void stopMtService() {
		
		logger.info("短信平台下行短信处理服务正在关闭...");
		
		if (mtSmsProcessingTask != null) {
			mtSmsProcessingTask.destroy();
			mtSmsProcessingTask = null;
		} else {
			logger.info("短信平台下行短信处理服务没有启动");
		}
	}
	
	/**
	 * 启动禁发队列短信处理服务
	 */
	public static void startFbService() {
		
		logger.info("短信平台禁发队列处理服务正在启动...");
		
		if (forbidProcessingTask == null || !forbidProcessingTask.isAlive()) {
			forbidProcessingTask = new ForbidProcessingTask();
			forbidProcessingTask.start();
		}
		
		logger.info("短信平台禁发队列处理服务启动完成");
	}
	
	/**
	 * 关闭禁发队列短信处理服务
	 */
	public static void stopFbService() {
		
		logger.info("短信平台禁发队列处理服务正在关闭...");
		
		if (forbidProcessingTask != null) {
			forbidProcessingTask.destroy();
			forbidProcessingTask = null;
		} else {
			logger.info("短信平台禁发队列处理服务没有启动");
		}
	}
	
	/**
	 * 开启上行短信处理服务
	 */
	public static void startMoService() {
		
		logger.info("短信平台上行短信处理服务正在启动...");
		
		if (moSmsProcessingTask == null || !moSmsProcessingTask.isAlive()) {
			moSmsProcessingTask = new MoSmsProcessingTask();
			moSmsProcessingTask.start();
		}
		
		logger.info("短信平台上行短信处理服务启动完成");
		
	}
	
	/**
	 * 关闭上行短信处理服务
	 */
	public static void stopMoService() {
		
		logger.info("短信平台上行短信处理服务正在关闭...");
		
		if (moSmsProcessingTask != null) {
			moSmsProcessingTask.destroy();
			moSmsProcessingTask = null;
		} else {
			logger.info("短信平台上行短信处理服务没有启动");
		}
	}
	
	/**
	 * 重建优先级队列
	 */
	public static void rebuildPriList() {
		
		logger.info("短信平台开始重构优先级队列...");
		
		// 重构优先级队列
		if (rebuildPriListTask == null || !rebuildPriListTask.isAlive()) {
			rebuildPriListTask = new RebuildPriListTask();
	    	rebuildPriListTask.start();
		}
	}
	
	public static void startOtherTasks() {
		
		// 获取优先级队列长度
		if (getPriListLengthTask == null || !getPriListLengthTask.isAlive()) {
			getPriListLengthTask = new GetPriListLengthTask();
			getPriListLengthTask.start();
		}
		
		if (smAffixZsetTask == null || !smAffixZsetTask.isAlive()) {
			smAffixZsetTask = new SMAffixZsetTask();
			smAffixZsetTask.start();
		}
		
		// 启动配置重载任务
		if (AppStaticConfigs.reloadConfigTimingOnOff) {
			if (reloadConfigTimingTask == null || !reloadConfigTimingTask.isAlive()) {
				reloadConfigTimingTask = new ReloadConfigTimingTask();
				reloadConfigTimingTask.start();
			}
		}
		
		if(routeFaultCheckTask == null || ! routeFaultCheckTask.isAlive()){
			routeFaultCheckTask = new RouteFaultCheckTask();
			routeFaultCheckTask.start();
		}
		
		if(gatewayAutoSwitchTask == null || ! gatewayAutoSwitchTask.isAlive()){
			gatewayAutoSwitchTask = new GatewayAutoSwitchTask();
			gatewayAutoSwitchTask.start();
		}
		
		if(wranSmsSocketTask == null || ! wranSmsSocketTask.isAlive()){
			wranSmsSocketTask = new WranSmsSocketTask();
			wranSmsSocketTask.start();
		}
		
		if(smsCallScanTask == null || ! smsCallScanTask.isAlive()){
			smsCallScanTask = new SmsCallScanTask();
			smsCallScanTask.start();
		}
	}
	
	public static void stopOtherTasks() {
		
		if (getPriListLengthTask != null) {
			getPriListLengthTask.destroy();
			getPriListLengthTask = null;
		}
		
		if (smAffixZsetTask != null) {
			smAffixZsetTask.destroy();
			smAffixZsetTask = null;
		}
		
		if (reloadConfigTimingTask != null) {
			reloadConfigTimingTask.destroy();
			reloadConfigTimingTask = null;
		}
		
		if (routeFaultCheckTask != null){
			routeFaultCheckTask.destroy();
			routeFaultCheckTask = null;
		}
		
		if(gatewayAutoSwitchTask != null){
			gatewayAutoSwitchTask.destroy();
			gatewayAutoSwitchTask = null;
		}
		
		if (wranSmsSocketTask != null){
			wranSmsSocketTask.destroy();
			wranSmsSocketTask = null;
		}
		
		if(smsCallScanTask != null){
			smsCallScanTask.destroy();
			smsCallScanTask = null;
		}
	}
}
